package cm.mefobe.android.telecomcost.view;

import java.security.InvalidParameterException;
import java.util.ArrayList;

import cm.mefobe.android.telecomcost.entities.MonthlyThreshold;
import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

public class MonthlyThresholdEditView extends LinearLayout {

	private Context _context;
	private MonthlyThreshold threshold;
	private ArrayList<WeeklyThresholdEditView> thresholdEditViews;

	public void setThreshold(MonthlyThreshold _threshold) {
		if (_threshold != null) {
			this.threshold = _threshold;
			thresholdEditViews = new ArrayList<WeeklyThresholdEditView>();
			
			Log.i("MonthlyThresholdEditView",
			"loading WeeklyThreshold for..." + threshold.getMonth().toString());
			ArrayList<WeeklyThreshold> thresholds = threshold
					.getWeeklyThresholds();

			Log.i("MonthlyThresholdEditView",
					"creating views from threshold...");
			int index = 0;
			while (index < thresholds.size()) {
				Log.i("MonthlyThresholdEditView",
						"creating view for weekly threshold..."
								+ thresholds.get(index).getWeek().toString());
				WeeklyThresholdEditView view = new WeeklyThresholdEditView(
						_context, thresholds.get(index));
				thresholdEditViews.add(view);
				index++;
			}

			setView();
		} else {
			throw new InvalidParameterException(
					"cannot set a null MonthlyThreshold");
		}
	}

	public MonthlyThreshold getThreshold() {
		return threshold;
	}

	public void setThresholdEditViews(
			ArrayList<WeeklyThresholdEditView> thresholdEditViews) {
		this.thresholdEditViews = thresholdEditViews;
	}

	public ArrayList<WeeklyThresholdEditView> getThresholdEditViews() {
		return thresholdEditViews;
	}

	public MonthlyThresholdEditView(Context context) {
		super(context);
		Log.i("MonthlyThresholdEditView", "creating the picker view");
		_context = context;
		setView();
	}

	public MonthlyThresholdEditView(Context context,
			ArrayList<WeeklyThresholdEditView> _thresholdEditViews) {
		super(context);
		Log.i("MonthlyThresholdEditView", "creating the picker view");
		_context = context;
		thresholdEditViews = _thresholdEditViews;
		setView();
	}

	private void setView() {
		if (isInEditMode()) {
			Log.i("MonthlyThresholdEditView", "isInEditMode");
		} else {
			removeAllViews();
			if (thresholdEditViews != null) {
				Log.i("MonthlyThresholdEditView",
						"adding thresholdEditViews...");
				int index = 0;
				while (index < thresholdEditViews.size()) {
					addView(thresholdEditViews.get(index));
					index++;
				}

				setOrientation(LinearLayout.VERTICAL);
			} else {
				Log.i("MonthlyThresholdEditView",
						"cannot setView....thresholdEditViews is null...");
			}
		}
	}

	public MonthlyThresholdEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("MonthlyThresholdEditView",
				"creating the picker view with AttributeSet");

		_context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("MonthlyThresholdEditView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		// setView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("MonthlyThresholdEditView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("MonthlyThresholdEditView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("MonthlyThresholdEditView", "onFinishInflate");
		super.onFinishInflate();
		setView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("MonthlyThresholdEditView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}
}